(function(){
    const searchParams=new URL(location.href).searchParams;
    var initPage = function (){
        Vue.app({
            data(){
                return {
                    file:{
                        url:'',
                        name:''
                    },
                    status:-1,
                    error0:"",
                    error1:"",
                    error2:"",
                    jd:""
                }
            },
            methods: {
                initEnv:function (){
                    var that = this;
                    var viewer;
                    var options = {
                        env: "AutodeskProduction",
                        api: 'derivativeV2',  // for models uploaded to EMEA change this option to 'streamingV2_EU'
                        getAccessToken: async function(onTokenReady) {
                            const xhr2 = await httpUtil.proxy("https://viewer-api.autodesk.com/viewer/v1/token/public","GET",
                                {},null,null,
                                {}
                            );
                            const res = JSON.parse(xhr2.responseText);
                            onTokenReady(res.accessToken,res.expiresIn);
                        }
                    };
                    Autodesk.Viewing.Initializer(options, function() {
                        var htmlDiv = document.getElementById('forgeViewer');
                        viewer = new Autodesk.Viewing.GuiViewer3D(htmlDiv);
                        var startedCode = viewer.start();
                        if (startedCode > 0) {
                            utils.$.errorMsg("当前浏览器不支持WebGL,请更换浏览器");
                            return;
                        }
                        that.status = 0;
                        that.viewer = viewer;
                        that.getAutoCadFile();
                    });
                },
                viewDoc:function (fileId){
                    const that = this;
                    return new Promise(function (success,error){
                        var documentId = "urn:"+fileId;
                        Autodesk.Viewing.Document.load(documentId, onDocumentLoadSuccess, onDocumentLoadFailure);
                        function onDocumentLoadSuccess(viewerDocument) {
                            that.status = 3;
                            success(true);
                            var defaultModel = viewerDocument.getRoot().getDefaultGeometry();
                            that.viewer.loadDocumentNode(viewerDocument, defaultModel);
                        }
                        function onDocumentLoadFailure() {
                            success(false);
                        }
                    });
                },
                initData:function (fn){
                    var that = this;
                    var path = searchParams.get("path");
                    that.file.path = path;
                    var url = parent.core.path2url(path);
                    console.log(url);
                    that.file.url = url;
                    that.file.name = searchParams.get("name");
                    if(!that.file.name){
                        const tmp = decodeURIComponent(url).split("/");
                        that.file.name = tmp[tmp.length-1];
                    }
                    const ext = autocad.getExtByName(that.file.name);
                    that.file.name = utils.uuid()+"."+ext;
                    if(searchParams.get("file_id")){
                        that.file.file_id = searchParams.get("file_id");
                    }
                    fn();
                },
                getAutoCadFile:async function (){
                    var that = this;
                    let webosUrl = localStorage.getItem("tenfellAutocad_webosUrl");
                    let autoCadPath = localStorage.getItem("tenfellAutocad_autoCadPath");
                    autocad.webosUrl = webosUrl;
                    autocad.autoCadPath = autoCadPath;
                    await autocad.getCadFileId(that.file,that.status,function (status,error){
                        that.status = status;
                        that["error"+that.status] = error;
                    },function (jd){
                        that.jd = jd;
                    },function (fileId){
                        fetch("../api.php?type=2&path="+encodeURIComponent(that.file.path?that.file.path:that.file.url)+"&file_id="+fileId);
                        return that.viewDoc(fileId);
                    });
                }
            },
            mounted:function(){
                var that = this;
                that.initData(that.initEnv);
            }
        });
    };
    utils.documentReady(function (){
        initPage();
    })
})()